//
//  SearchLineMode.m
//  dataGathering
//
//  Created by apple on 14/12/24.
//  Copyright (c) 2014年 shuili. All rights reserved.
//

#import "SearchLineMode.h"

@interface SearchLineMode ()
@property(nonatomic, retain)AGSPoint* beginPoint;
@property(nonatomic, retain)AGSMutablePolyline* myPolyline;
@property(nonatomic, retain)AGSSimpleLineSymbol* innerLineSymbol;

@end

@implementation SearchLineMode

-(instancetype)initWithMapview :(AGSMapView*)mapview
{
    self = [super initWithMapview:mapview];
    if(self){
        
        _innerLineSymbol = [AGSSimpleLineSymbol simpleLineSymbolWithColor:[[UIColor blueColor] colorWithAlphaComponent:0.80] width:5.0];
        _myPolyline = [[AGSMutablePolyline alloc] initWithSpatialReference:self.mapview.spatialReference];

        [_myPolyline addPathToPolyline];
    }
    return self;
}

-(void)mapView:(AGSMapView *)mapView didTapAndHoldAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features
{
    if([self.myPolyline numPoints] > 0)
    {
        while([self.myPolyline numPoints] > 0){
            [self.myPolyline removePointOnPath:0 atIndex:0];
        }
        [self.myGraphic setGeometry:self.myPolyline];
        _beginPoint = mappoint;
        [self.myPolyline addPointToPath:mappoint];
    } else {
        self.myGraphic = [[AGSGraphic alloc] initWithGeometry:mappoint symbol:self.innerSymbol attributes:nil];
        [self.myGraphicsLayer addGraphic:self.myGraphic];
        _beginPoint = mappoint;
        [self.myPolyline addPointToPath:mappoint];
    }
    
}

-(void)mapView:(AGSMapView *)mapView didMoveTapAndHoldAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features
{
    NSLog(@"moving");
    
    while([_myPolyline numPoints] > 1){
        [_myPolyline removePointOnPath:0 atIndex:([self.myPolygon numPoints]-1)];
    }

    [_myPolyline addPointToPath:mappoint];
    [self.myGraphic setGeometry:_myPolyline];
    
}

-(void)mapView:(AGSMapView *)mapView didEndTapAndHoldAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features
{
    NSLog(@"end");
    
    while([_myPolyline numPoints] > 1){
        [_myPolyline removePointOnPath:0 atIndex:([self.myPolygon numPoints]-1)];
    }
    
    [_myPolyline addPointToPath:mappoint];

    
    //[self stop];
    
    //if (self.delegate && [self.delegate respondsToSelector:@selector(SearchBaseMode:didFinishDrawing:)]) {
    //    [self.delegate SearchBaseMode:self didFinishDrawing:_myPolyline];
    //}
}

-(void)mapView:(AGSMapView *)mapView didClickAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features
{
    
    if([[AGSGeometryEngine defaultGeometryEngine] geometry:mappoint intersectsGeometry:self.myPolyline])
    {
        [self stop];
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(SearchBaseMode:didFinishDrawing:)]) {
            [self.delegate SearchBaseMode:self didFinishDrawing:_myPolyline];
        }
    }
}

@end
